#include <cstdio>
#include <iostream>
using namespace std;

#include "basethread.h"

#include "sessionsocket.h"
#include "listensocket.h"
#include "selectpoll.h"
#include "connection.h"

using   namespace SocketLib;

class MySession : public SessionSocket
{
public:
    void    onConnection()
    {
    }

    void    onRecvComplete(char* pBuffer, int len)
    {
        m_recvBuffer.writeBuffer(pBuffer, len);
    }

    void    onClientDisConnection()
    {
        printf("client close\n");
    }
};

template<typename SessionSocketType>
void     initAllSessionSocket(ListenSocket* listensocket, int num)
{
    SessionSocket* s_socketFDArray = new SessionSocketType[num];
    for (int i = 0; i < num; i++)
    {
        s_socketFDArray[i].setListenSocket(listensocket);
        listensocket->addFreeSocketFD(&s_socketFDArray[i]);
    }
}

int main()
{
    //test socket
    {
        ListenSocket    l;
        initAllSessionSocket<MySession>(&l, 1000);

        l.listen(5779, 5);

        SelectPoll sl;
        sl.addSocketFD(&l);
        l.setPoll(&sl);

        while(1)
        {
            sl.doPoll();
            ThreadLib::YieldThread(1);
        }
    }

    return 0;
}